package com.pengheng.transformation.unionstream;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

public class ConnectStreamDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        DataStreamSource<Integer> source1 = env.fromElements(1, 2, 3);
        DataStreamSource<String> source2 = env.fromElements("a", "b", "c");

//        SingleOutputStreamOperator<Integer> source1 = env.socketTextStream("192.168.10.100", 7777).map(Integer::parseInt);
//        SingleOutputStreamOperator<String> source2 = env.socketTextStream("192.168.10.100", 8888);


        /**
         * 只能将两个数据流合并
         * 流的数据类型可以不一样
         * 连接后可以调用map、flatmap、process来处理，但是各自处理各自的
         */
        ConnectedStreams<Integer, String> connect = source1.connect(source2);
        SingleOutputStreamOperator<String> result = connect.map(new CoMapFunction<Integer, String, String>() {
            @Override
            public String map1(Integer value) throws Exception {
                return "来源于数字流：" + value;
            }

            @Override
            public String map2(String value) throws Exception {
                return "来源于字母流：" + value;
            }
        });
        result.print();

        env.execute();
    }
}
